package com.hboxs.asl.controller.app.product;

import com.hboxs.asl.*;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Set;

@Controller(value = "appTeacherControl")
@RequestMapping(value = "/app/teacher")
public class TeacherControl extends BaseController {

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "categoryServiceImpl")
    private CategoryService categoryService;
    @Resource(name = "caseServiceImpl")
    private CaseService caseService;
    @Resource(name = "reviewServiceImpl")
    private ReviewService reviewService;

    /**
     * 获取老师列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list(Long categoryId, Pageable pageable) {

        Category category = categoryService.find(categoryId);
        if(null == category) return JsonMessage.error("找不到该分类");

        Page<Member> page = memberService.findTeacherByCategory(pageable, category);

        return JsonMessage.success(

                "totalPage", page.getTotalPages(),

                "pageNumber", page.getPageNumber(),

                "pageSize", page.getPageSize(),

                "total", page.getTotal(),

                "content", JsonMessage.optimiseObj(
                        page.getContent(),
                        "id", "name", "avator", "introduction", "tags", "ID"
                )

        );
    }

    /**
     * 更多老师详情
     *
     * @return JsonMessage
     */
    @RequestMapping(value = "/showDetail", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage showDetail(Long teacherId, Pageable pageable) {
        try {
            Member teacher = memberService.find(teacherId);
            if (teacher == null) {
                return JsonMessage.error("找不到该老师");
            }
            Page<Case> pageCases = caseService.fingCasePage(pageable, teacher);
            return JsonMessage.success(

                    "teacher", JsonMessage.optimiseObj(teacher, "avator", "name", "introduction", "tags", "memberCertification.qualificationImages", "background"),

                    "totalPage", pageCases.getTotalPages(),

                    "pageNumber", pageCases.getPageNumber(),

                    "pageSize", pageCases.getPageSize(),

                    "total", pageCases.getTotal(),

                    "content", JsonMessage.optimiseObj(
                            pageCases.getContent(),
                            "title", "imageURL", "id", "createDateF", "content", "contentWithoutTag"
                    )
            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 老师详情
     * @param teacherId
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage detail(Long teacherId) {
        try {
            Member teacher = memberService.find(teacherId);
            if(teacher == null) {
                return JsonMessage.error("找不到该老师");
            }
            Member member = memberService.getCurrentByToken();
            boolean attention = false;
            if (member != null) {
                Set<Member> memberSet = member.getFavorAnchors();
                if(memberSet.contains(teacher)) {
                    attention = true;
                }
            }
            //获取综合评价
            NumberFormat formatter = new DecimalFormat("0");
            double serviceScore = reviewService.serviceScore(teacher);
            double schemeScore = reviewService.schemeScore(teacher);
            double feedbackScore = reviewService.feedbackScore(teacher);
            double avg = (serviceScore+schemeScore+feedbackScore)/3*2*10;
            if (avg==0) {
                avg = 100;
            }
            String evaluation = formatter.format(avg);
            return JsonMessage.success(

                    "id", teacher.getId(),

                    "evaluation", evaluation,

                    "attention", attention,

                    "avator", teacher.getAvator(),

                    "name", teacher.getName(),

                    "ID", teacher.getID(),

                    "myFansNumber", teacher.getFavorMembers().size(),

                    "introduction",teacher.getIntroduction(),

                    "tag", teacher.getTags(),

                    "background", teacher.getBackground()
            );


        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 根据老师昵称搜索
     * @param name
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage search (String name, Pageable pageable) {
        try {
            Page<Member> page = memberService.searchTeacherPage(pageable, name);
            return JsonMessage.success(

                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "id", "name", "avator", "introduction", "tags", "ID"
                    )

            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }



    /**
     * 老师案例详情
     *
     * @param id 案例id
     * @return
     */
    @RequestMapping(value = "/caseWeb", method = RequestMethod.GET)
    public
    String detail(Long id, ModelMap modelMap) {

        Case example = caseService.find(id);
        if (example == null) {
            return "/yijing/404";
        }
        modelMap.addAttribute("case" , example);
        return "/yijing/caseWeb";
    }







}
